from Selenium2Library.keywords.keywordgroup import KeywordGroup
from Selenium2Library.locators import ElementFinder
from _breeze_account_summary import _BreezeAccountSummaryKeywords
from _breeze_mail import _BreezeMailKeywords
from _breeze_payee_list import _BreezePayeeListKeywords
from _breeze_transaction_list import _BreezeTransactionListKeywords
from _breeze_transfer import _BreezeTransferKeywords
import time
import robot

class _BreezeCommonKeywords(_BreezeAccountSummaryKeywords, _BreezeMailKeywords, _BreezePayeeListKeywords, _BreezeTransactionListKeywords, _BreezeTransferKeywords):
    """Library for Breeze Common Keywords purposes.
    
    This library is used in Breeze projects.
    """
    def __init__(self):
        """Does nothing. init only"""
        print '_BreezeCommonKeywords.__init__'
        
    def select_frame(self, locator):
        """Sets frame identified by `locator` as current frame.

        Key attributes for frames are `id` and `name.` See `introduction` for
        details about locating elements.
        """
        self._info("Selecting frame '%s'." % locator)
        element = self._element_find(locator, True, True, tag='frame')
        self._current_browser().switch_to_frame(element)
        
    def select_iframe(self, locator):
        """Sets frame identified by `locator` as current frame.

        Key attributes for frames are `id` and `name.` See `introduction` for
        details about locating elements.
        """
        self._info("Selecting frame '%s'." % locator)
        element = self._element_find(locator, True, True, tag='iframe')
        self._current_browser().switch_to_frame(element)
    
        
    def select_top_frame_ibanking(self):
        '''Used for ID iBanking to select to top frame
        '''
        self._current_browser().switch_to_default_content()
        self.select_frame("//frame[@id='CenterContent']")
        self.select_iframe("//iframe[@id='iframename']")
        self.select_frame("//frame[@id='loginFrame']")
        self.select_frame("//frame[@name='loginTopFrame']")

    def select_links_frame_ibanking(self):
        """Used for ID iBanking to select to links frame which is at right side.
        Navigation bar.
        """
        self._current_browser().switch_to_default_content()
        self.select_frame("//frame[@id='CenterContent']")
        self.select_iframe("//iframe[@id='iframename']")
        self.select_frame("//frame[@id='loginlinksFrame']")
        
    def wait_until_page_not_contains(self, text, timeout=None, error=None):
        """Waits until `text` not appears on current page.

        Fails if `timeout` expires before the text appears. See
        `introduction` for more information about `timeout` and its
        default value.

        `error` can be used to override the default error message.

        See also `Wait Until Page Contains Element`, `Wait For Condition` and
        BuiltIn keyword `Wait Until Keyword Succeeds`.
        """
        if not error:
            error = "Text '%s' did not appear in <TIMEOUT>" % text
        self._wait_until_not(timeout, error, self._is_text_present, text)


    def _wait_until_not(self, timeout, error, function, *args):
        timeout = robot.utils.timestr_to_secs(timeout) if timeout is not None else self._timeout_in_secs
        error = error.replace('<TIMEOUT>', robot.utils.secs_to_timestr(timeout))
        maxtime = time.time() + timeout
        while function(*args):
            if time.time() > maxtime:
                raise AssertionError(error)
            time.sleep(0.2)

    def should_be_equal_for_each(self,bmw_ls,ibank_ls):

        flag = 0
        if len(bmw_ls) == len(ibank_ls):
            for i in xrange(len(bmw_ls)):
                if bmw_ls[i][0] != ibank_ls[i][0]:
                    self._info("bmw & ibank is not the same"+bmw_ls[i][0]+ibank_ls[i][0])
                    flag = 1
                if bmw_ls[i][1] != ibank_ls[i][1]:
                    self._info("bmw & ibank is not the same"+bmw_ls[i][1]+ibank_ls[i][1])
                    flag = 1
                    
            if flag == 1:
                raise AssertionError('not equal')
        else:
            raise AssertionError("miss field")
        
        
    def read_2fa_ibanking(self):
        tab_user_id = self._get_text("//*/table[2]/tbody/tr[3]/td[2]")
        tab_message_content = self._get_text("//*/table[2]/tbody/tr[3]/td[4]")
        tab_sms_type = self._get_text("//*/table[2]/tbody/tr[3]/td[5]")
        tab_trxn_type = self._get_text("//*/table[2]/tbody/tr[3]/td[6]")
        self._info(tab_user_id)
        self._info(tab_message_content)
        self._info(tab_sms_type)
        self._info(tab_trxn_type)
        if 'ALP' in tab_sms_type :
            return tab_message_content.split(' ')[5].replace('.', '')

    def read_otp_ibanking(self):
        tab_user_id = self._get_text("//*/table[2]/tbody/tr[3]/td[2]")
        tab_message_content = self._get_text("//*/table[2]/tbody/tr[3]/td[4]")
        tab_sms_type = self._get_text("//*/table[2]/tbody/tr[3]/td[5]")
        tab_trxn_type = self._get_text("//*/table[2]/tbody/tr[3]/td[6]")
        self._info(tab_user_id)
        self._info(tab_message_content)
        self._info(tab_sms_type)
        self._info(tab_trxn_type)
        if 'OTP' in tab_sms_type :
            return tab_message_content.split(' ')[8].replace('.', '')
